/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import controller.commands.AddNodeCommand;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import model.graphic.GraphicModel;
import view.GUI.sidepanels.PackageItem;
import view.GUI.sidepanels.Palette;
import view.GUI.sidepanels.PaletteItem;
import view.GUI.sidepanels.TransferableArrayList;

/**
 *
 * @author Jelle, modified by Ben
 */
public class Reflect {

    private String dirTo = "src\\load\\";
    private GraphicModel gmodel;
    private TransferableArrayList commands;
    private Palette palette;

    public Reflect(GraphicModel gmodel, Palette palette) {
        this.gmodel = gmodel;
        this.palette = palette;
    }

    public void initLoad() {
        String diro = "src\\load";
        File dir = new File(diro);
        for (File container : dir.listFiles()) {
            if (!container.isHidden() && !container.getName().startsWith(".")&& !container.getName().equals("parent")) {
                PackageItem lbl = new PackageItem(container.getName());
                palette.addLabel(lbl);
                for (File f : container.listFiles()) {
                    if (!f.isHidden() && !f.getName().startsWith(".")) {
                        String name = f.getName().substring(0, f.getName().length() - 5);
                        commands = init("load." + container.getName() + "." + name);
                        PaletteItem pi = new PaletteItem(commands, name, container.getName(), Constants.getInfoAbout(name));
                        pi.addInfoPanelRef(palette.getInfoPanelRef());
                        palette.add(pi);
                    }
                }
            }
        }
        PackageItem lbl = new PackageItem("custom");
        palette.addLabel(lbl);
    }

    public TransferableArrayList init(String f) {
        commands = new TransferableArrayList();
        try {
            Class<?> c = Class.forName(f);
            System.out.println("c.getsimplemname" + c.getSimpleName());
            commands.add(new AddNodeCommand(gmodel, c));
        } catch (Exception ex) {
            System.out.println("EX3 " + ex.getMessage() + " - " + ex.getCause());
        }
        return commands;
    }

    public void importNode(File f) {
        try {
            FileReader fr = new FileReader(f.getAbsolutePath());
            OutputStream out = new FileOutputStream(dirTo + "imported\\" + f.getName());
            BufferedReader br = new BufferedReader(fr);
            String line;
            while ((line = br.readLine()) != null) {
                if (line.contains("package")) {
                    line = "package load.imported;";
                }
                line += "\n";
                out.write(line.getBytes());
            }

            br.close();
            out.close();
            fr.close();

        } catch (Exception ex) {
            System.out.println("Reflect loadConnectClasses EXCEPTION " + ex.getMessage());
        }
        //Compile import
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int result = compiler.run(null, null, null, dirTo + "imported\\" + f.getName());
        System.out.println("Compile result code = " + result);

        //Move compiled .class files
        File dirdest = new File("build\\classes\\load\\imported");
        File dir = new File("src\\load\\imported");
        for (File file : dir.listFiles()) {
            // Destination directory
            if(file.getName().endsWith("class")){
                // Move file to new directory
                boolean success = file.renameTo(new File(dirdest, file.getName()));
                if (!success) {
                    file.delete();
                }
            }
        }
        palette.removeAll();
        initLoad();
    }

}
